/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Config;

import CrlException.CrlException;
import ExtractorInterface.ExtractorData;
import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 *
 * @author CUHUUHOANG
 */
public class ConfigCtrl {

    public void getDataFromExtractor(ExtractorData exData) throws CrlException {
        try {
            ConfigSetting.seedURL = exData.getSeedURL();
            ConfigSetting.tableName = exData.getTableName();
            ConfigSetting.allAttribute = exData.getAtributes();
            ConfigSetting.imgAttriName = exData.getImageAtrributeName();
        } catch (CrlException ex) {
            throw new CrlException(ex.getMessage());
        }
    }

    public int checkValidMaxThread(String inpMaxThread) throws CrlException {
        int out = 0;
        try {
            out = Integer.parseInt(inpMaxThread);
            if (out < 1) {
                throw new CrlException("Max Thread can not be smaller than 1");
            }
        } catch (NumberFormatException ex) {
            throw new CrlException("Wrong format of max Thread");
        }
        return out;
    }

    public int checkValidDepth(String depth) throws CrlException {
        int out = 0;
        try {
            out = Integer.parseInt(depth);
            if (out < 0) {
                throw new CrlException("Depth can not be smaller than 0");
            }
        } catch (NumberFormatException ex) {
            throw new CrlException("Wrong format of depth");
        }
        return out;
    }

    public void checkValidDatabase(String hostMySQL, String databaseName, String username, String password) throws CrlException {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            throw new CrlException("Can not find MySQL JDBC Driver");
        }
        Connection connection = null;

        try {
            connection = DriverManager
                    .getConnection(hostMySQL + databaseName, username, password);

        } catch (SQLException e) {
            throw new CrlException("Connection to database fail");
        }

        if (connection == null) {
            throw new CrlException("Connection to database fail");
        }
    }

    public void checkValidExtractor(String extractorPath) throws CrlException {
        try {
            String absExtractorPath= getAbsPath(extractorPath);
            File file = new File(absExtractorPath);
            URL url = file.toURL();
            URL[] urls = new URL[]{url};

            ClassLoader loader = URLClassLoader.newInstance(
                    urls,
                    getClass().getClassLoader());

            Class<?> clazz = Class.forName("CrawlerExtractor.Data", true, loader);
            clazz = Class.forName("CrawlerExtractor.Extractor", true, loader);

        } catch (Exception e) {
            throw new CrlException("Can not load extractor");
        }
    }

    public void checkImageDir(String imageDir) throws CrlException {
        String absImageDir= getAbsPath(imageDir);
        File file = new File(absImageDir);
        if (!file.exists() || !file.isDirectory()) {
            throw new CrlException("Can not open image folder");
        }
    }
    
    public boolean checkValidBoolean(String bool) throws CrlException{
        boolean out=false;
        try{
            out=Boolean.parseBoolean(bool);
        }catch(Exception ex){
            throw new CrlException("Wrong format");
        }
        return out;    
    }
    
    public int checkValidPort(String port) throws CrlException{
        int out=0;
        try{
            out=Integer.parseInt(port);
        }catch(NumberFormatException ex){
            throw new CrlException("Wrong format port");
        }
        return out;    
    }
    
    public static String getAbsPath(String relativePath){
        if(relativePath.length()<1) return relativePath;
        if(relativePath.charAt(0)=='.')
        {
            return new File("").getAbsolutePath()+ relativePath.substring(1);
        }
        else return relativePath;
    }
    
}
